import { _decorator, Component, Animation, Sprite, UITransform } from 'cc'
import { WOODEN_SKULL_STATE, WoodenSkullStateMachine } from './WoodenSkullStateMachine'
import { DataCenter } from '../../Runtime/DataCenter'
const { ccclass, property } = _decorator

@ccclass('WoodenSkullController')
export class WoodenSkullController extends Component {
  //人物当前的位置
  x: number = 6
  y: number = 5

  _fsm: WoodenSkullStateMachine = null

  init() {
    console.log('WoodenSkullController init')
    this.createEntitySprite()
    //初始化状态机
    this._fsm = new WoodenSkullStateMachine(this.getComponent(Animation))
    this._fsm.stateName = WOODEN_SKULL_STATE.IDLE_LEFT
  }

  //创建人物精灵
  async createEntitySprite() {
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM
    const transform = this.node.getComponent(UITransform)
    transform.setContentSize(DataCenter.TILE_WIDTH * 4, DataCenter.TILE_HEIGHT * 4)
    this.node.setPosition(
      this.x * DataCenter.TILE_WIDTH - DataCenter.TILE_WIDTH * 0.5,
      -(this.y * DataCenter.TILE_HEIGHT - DataCenter.TILE_HEIGHT * 0.5),
    )

    this.node.addComponent(Animation)
  }
}
